package com.example.demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class kien extends Activity {
	private String port;
	private String IP;
	private Button on;
	private Button off;
	private OutputStreamWriter OutPut;
	Socket connectServer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.huychanh);
        	on = (Button) findViewById(R.id.buttonon);
        	off= (Button) findViewById(R.id.buttonoff);
        Bundle receiverBundle=this.getIntent().getExtras();
        port =receiverBundle.getString("portString");
        IP   =receiverBundle.getString("ipString");
        try {
			connectServer= new Socket(IP,Integer.parseInt(port));
			
		      
             System.out.println("co chay khong");
             //su ly nut on
	    	on.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					sendRequire("on");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		  
			}
		})	;
	    	//su li nut off
			off.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					try {
						sendRequire("off");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			});
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }

   /* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.duychanh, menu);
        return true;
    }*/
    public void sendRequire(String msg) throws IOException{
    	OutPut = new OutputStreamWriter(this.connectServer.getOutputStream());
    	OutPut.write(msg);
    	OutPut.flush();
    	
    }
}
